//
//  MainViewController.swift
//  HMWeibo
//
//  Created by HuaMou.Chen on 15/11/26.
//  Copyright © 2015年 HuaMou.Chen. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    //MARK: - 监听撰写按钮点击
    @objc private func composeButtonClick() {
        print("点击撰写按钮")
    }

    //MARK: - 界面设置
    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()
        setupComposeButton()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.bringSubviewToFront(composeButton)
    }
    
    
    /// 设置撰写按钮
    private func setupComposeButton() {
        tabBar.addSubview(composeButton)
        //设置frame
        let count = self.childViewControllers.count
        let w = tabBar.bounds.width / CGFloat(count) - 1
        composeButton.frame = CGRectInset(tabBar.bounds, w * 2, 0)
        composeButton.addTarget(self, action: "composeButtonClick", forControlEvents: .TouchUpInside)
        
    }
    
    /// 添加所有子控制器
    private func addChildViewControllers() {
        
         addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        
         addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
         addChildViewController(DiscoveTableViewController(), title: "发现", imageName: "tabbar_discover")
        
         addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    //// 添加子控制器
    ///
    /// - parameter vc:        控制器
    /// - parameter title:     标题
    /// - parameter imageName: 图片名称
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        addChildViewController(UINavigationController(rootViewController: vc))
    }
    
    // MARK: - 懒加载控件
    private lazy var composeButton: UIButton = UIButton(imageName: "tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
        
}
